package com.au.bescene.activity;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.au.bescene.R;
import com.au.bescene.adapter.MainMenuAdapter;

/**
 * Represents the Main Menu screen.
 * @author Khaled
 *
 */
public class MainMenu extends ListActivity {

	private String[] _menu;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.mainmenu);

		_menu = new String[] { 
	        	getResources().getString(R.string.main_menu_all_events),
	        	getResources().getString(R.string.main_menu_photo_gallery),
	        	getResources().getString(R.string.main_menu_pubs_clubs_bars)
	        	};
		
		MainMenuAdapter adapter = new MainMenuAdapter(this, _menu);
		setListAdapter(adapter);
		
	}
	
	/**
	 * Listens to click events for Main Menu items.
	 */
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
	
		if (position == 0) {
			if (isOnline()) {
				Intent intent = new Intent(MainMenu.this,
						EventList.class);
				startActivity(intent);
			} else
				Toast.makeText(getApplicationContext(),
						getResources().getString(R.string.internetrequired), Toast.LENGTH_LONG)
						.show();
		} else if (position == 1) {
			if (isOnline()) {
				Intent intent = new Intent(MainMenu.this, PhotoGalleryList.class);
				startActivity(intent);
			} else
				Toast.makeText(getApplicationContext(),
						getResources().getString(R.string.internetrequired), Toast.LENGTH_SHORT)
						.show();
		} else if (position == 2) {
			Intent intent = new Intent(MainMenu.this, VenueList.class);
			startActivity(intent);
		}
	}
	
	/**
	 * Checks if mobile phone is connected to the internet.
	 * @return network status.
	 */
	private boolean isOnline() {
	    ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnected()) {
	        return true;
	    }
	    return false;
	}
}
